﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using VietImage.Models;
using VietImage.Logs;

namespace VietImage.Roles
{
    public class CustomerRole : RegisteredUserRole
    {
        #region Attributes
        private Customer _customerInfo;
        #endregion

        #region Properties
        public Customer CustomerInfo
        {
            get { return this._customerInfo; }
        }
        #endregion

        #region Methods
		public CustomerRole()
		{
			this._customerInfo = new Customer();
		}
		
        public List<ErrorItem> CreateLightBox(Lightbox ob)
        {
            List<ErrorItem> errorList = Lightbox.Insert(ref ob, this.UserInfo.ID, this.ID_Language); ;

            if (errorList == null)
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " created lightbox.");
            else
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " cannot create lightbox.");

            return errorList;
        }

        public List<ErrorItem> EditLightBox(Lightbox ob)
        {
            List<ErrorItem> errorList = new List<ErrorItem>();

            errorList = Lightbox.ChangeInformation(ob, this.UserInfo.ID, 0, this.ID_Language);

            if (errorList == null)
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " changed lightbox.");
            else
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " cannot change lightbox.");

            return errorList;
        }

        public List<ErrorItem> DeleteLightBox(int id_Lightbox)
        {
            List<ErrorItem> errorList = new List<ErrorItem>();

            errorList = Lightbox.Delete(id_Lightbox, this.UserInfo.ID, 0, this.ID_Language);

            if (errorList == null)
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " deleted lightbox.");
            else
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " cannot delete lightbox.");

            return errorList;
        }

        public List<ErrorItem> InsertImageLightBox(int id_Image, int id_Lightbox)
        {
            List<ErrorItem> errorList = new List<ErrorItem>();

            Lightbox i = Lightbox.GetLightbox(id_Lightbox);
            errorList = i.InsertImage(id_Image, this.UserInfo.ID, this.ID_Language);

            if (errorList == null)
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " inserted image No." + id_Image + " to lightbox No." + id_Lightbox + " .");
            else
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " cannot insert image No." + id_Image + " to lightbox No." + id_Lightbox + " .");

            return errorList;
        }

        public List<ErrorItem> SetPrimaryImageLightBox(int id_Lightbox, System.Data.Linq.Binary image)
        {
            List<ErrorItem> errorList = new List<ErrorItem>();

            Lightbox i = Lightbox.GetLightbox(id_Lightbox);

            errorList = i.SetPrimaryImage(image, this.UserInfo.ID, this.ID_Language);

            return errorList;
        }

        public List<ErrorItem> RemoveImageLightBox(int id_Image, int id_Lightbox)
        {
            List<ErrorItem> errorList = new List<ErrorItem>();

            Lightbox i = Lightbox.GetLightbox(id_Lightbox);
            errorList = i.RemoveImage(id_Image, this.UserInfo.ID, this.ID_Language);

            if (errorList == null)
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " removed image No." + id_Image + " to lightbox No." + id_Lightbox + " .");
            else
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " cannot remove image No." + id_Image + " to lightbox No." + id_Lightbox + " .");

            return errorList;
        }

        public List<ErrorItem> DownloadLightBox(int id_Lightbox, int downloadKind, ref List<System.Drawing.Image> imgList)
        {
            List<ErrorItem> errorList = new List<ErrorItem>();

            imgList = new List<System.Drawing.Image>();

            Lightbox i = Lightbox.GetLightbox(id_Lightbox);

            foreach (Image img in i.Images)
            {
                System.Drawing.Image source = null;
                this.DownloadImage(img.ID, downloadKind, ref source);
                imgList.Add(source);
            }

            if (errorList == null)
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " downloaded lightbox No." + id_Lightbox + ".");
            else
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " cannot download lightbox No." + id_Lightbox + ".");

            return errorList;
        }

        public List<ErrorItem> DownloadImage(int id_Image, int downloadKind, ref System.Drawing.Image source)
        {
            List<ErrorItem> errorList = new List<ErrorItem>();

            Image img = Image.GetImage(id_Image);

            Byte[] myImage = img.Source.ToArray();
            System.IO.MemoryStream imgMemoryStream = new System.IO.MemoryStream(myImage);
            source = new System.Drawing.Bitmap(imgMemoryStream);

            errorList = img.Download(this.UserInfo.ID, downloadKind, this.ID_Language);

            if (errorList == null)
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " downloaded image No." + id_Image + ".");
            else
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " cannot download image No." + id_Image + ".");

            return errorList;
        }
        #endregion

        #region Reports
        public List<Image> GetImageListDownloaded(DateTime date)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();

            var list = from images in _db.Images
                       join logimages in _db.LogImages on images.ID equals logimages.ID_Image
                       where (logimages.ID_Action == 7 || logimages.ID_Action == 8 || logimages.ID_Action == 9) && logimages.ActionTime.Date == date.Date && logimages.ID_User == this.UserInfo.ID
                       select images;

            return list.OrderByDescending(p=>p.ID).ToList();
        }

        public List<Image> GetImageListDownloaded(DateTime beginDate, DateTime endDate)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();

            var list = from images in _db.Images
                       join logimages in _db.LogImages on images.ID equals logimages.ID_Image
                       where (logimages.ID_Action == 7 || logimages.ID_Action == 8 || logimages.ID_Action == 9) && logimages.ActionTime.Date >= beginDate.Date && logimages.ActionTime.Date <= endDate.Date && logimages.ID_User == this.UserInfo.ID
                       select images;

            return list.OrderByDescending(p=>p.ID).ToList();
        }

        public List<Image> GetImageListDownloaded(int month, int year)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();

            var list = from images in _db.Images
                       join logimages in _db.LogImages on images.ID equals logimages.ID_Image
                       where (logimages.ID_Action == 7 || logimages.ID_Action == 8 || logimages.ID_Action == 9) && logimages.ActionTime.Date.Month == month && logimages.ActionTime.Year == year && logimages.ID_User == this.UserInfo.ID
                       select images;

            return list.OrderByDescending(p=>p.ID).ToList();
        }
        #endregion
    }
}